<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: younghxp
 * @Date: 2022-03-14 14:36:06
 * @LastEditors: younghxp
 * @LastEditTime: 2022-03-28 17:51:29
-->
<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth">
        <el-form :inline="true" :model="filterField" ref="accountForm">
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptionsData"
                @change="getAreaId"
                :props="{ checkStrictly: true }"
                clearable
                filterable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业">
              <el-select
                v-model="filterField.industryId"
                filterable
                placeholder="请选择行业"
                clearable
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="历史项目归档">
                <el-select
                  v-model="filterField.isArchive"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option value="1" label="是"></el-option>
                  <el-option value="0" label="否"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-form-item>
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <el-button
              v-hasBtn="'smallProjectSet:add'"
              class="commonBut saveBtn funComBut addSecondTab"
              icon="icon iconfont icontianjia"
              @click="toRulesPage('add', '')"
            >
              <span class="opetareText">新建规则</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <!-- 列表 -->
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            align="center"
            prop="areaName"
            label="所属区域"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="industryName"
            label="行业"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="total"
            label="工程造价(元)"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="cycle"
            label="建设周期(天)"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="archiveCode"
            label="历史项目归档"
            min-width="120"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            align="left"
            prop="reason"
            label="备注"
            width="230px"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.reason"
                :columnName="'reason'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column align="left" label="操作" fixed="right" width="180">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-hasBtn="'smallProjectSet:detail'"
                @click="toRulesPage('look', scope.row)"
                >查看</el-link
              >
              <el-link
                type="primary"
                v-hasBtn="'smallProjectSet:update'"
                @click="toRulesPage('edit', scope.row)"
                >修改</el-link
              >
              <el-link
                type="primary"
                v-hasBtn="'smallProjectSet:delete'"
                @click="delConfirm(scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
    <el-dialog
      title="提示"
      width="640px"
      :visible.sync="showDel"
      :close-on-click-modal="false"
      @close="handleClose"
      :modal-append-to-body="false"
    >
      <div class="dia-content">
        删除该规则后，当前规则下的所有项目将重新归档，是否确认删除？
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleClose" class="cancel">取消</el-button>
        <el-button
          type="primary"
          @click.native="deleteRules"
          class="save"
          :loading="isDelLoading"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapState, mapActions } from 'vuex'

export default {
  name: 'ruleSetting',
  mixins: [scrollTable],
  data() {
    return {
      advanced: false,
      requestUrl: 'api-p/smallprojectrule/getListByPage',
      areaId: '',
      filterField: {
        areaId: '',
        industryId: '',
        isArchive: '',
      },
      id: '',
      assocId: '',
      tableData: [], //表格数据
      loading: true,
      isShow: false,
      dialogType: '',
      showDel: false,
      isDelLoading: false,
    }
  },
  created() {
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.getIndustry()
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  props: {
    index: {
      type: String,
    },
  },
  watch: {
    index(newVal) {
      if (newVal === '0') {
        this.$refs.tableBox1 && this.$refs.tableBox1.doLayout()
      }
    },
  },
  computed: {
    ...mapState(['industryOptions', 'areasOptions']),
    areasOptionsData() {
      const computedData = (arr) =>
        arr.forEach((v) =>
          v.value.length >= 6
            ? delete v.children
            : v.children && computedData(v.children)
        )
      computedData(this.areasOptions)
      return this.areasOptions
    },
  },
  methods: {
    ...mapActions(['getIndustry', 'getAreas']),
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    closeDialog(val) {
      if (val === 'update') {
        this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      }
    },
    // 添加
    toRulesPage(types, item) {
      this.dialogType = types
      this.$router.push({
        path: '/home/smallProjectManage/addSmallProjectRule',
        name: 'addSmallProjectRule',
        query: {
          types,
          id: item ? item.id : '',
          assocId: item ? item.assocId : '',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/smallProjectManage/smallProjectSet',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    delConfirm(item) {
      // this.showDel = true
      this.id = item.id
      this.assocId = item.assocId
      this.$confirm(
        '删除该规则后，当前规则下的所有项目将重新归档，是否确认删除？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          this.deleteRules()
        })
        .catch(() => {})
    },
    handleClose() {
      this.showDel = false
      this.id = ''
      this.assocId = ''
    },
    // 二次确认
    deleteRules() {
      this.$confirm('确认删除该规则?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.axios
            .post('api-p/smallprojectrule/removeSmallRule', {
              id: this.id,
              assocId: this.assocId,
            })
            .then((res) => {
              if (!res.data.hasError) {
                this.$message.success('删除成功')
                this.handleClose()
                this.searchList()
              } else {
                this.$message.error(response.data.errorMsg)
              }
            })
        })
        .catch(() => {})
    },
    // 删除
    delIpAddress() {},
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
.mainCon {
  top: 213px;
}
.dia-content {
  font-size: 16px;
  line-height: 36px;
  font-weight: 600;
}
</style>
